<?php
/**
 * CRMLite Announce Controller
 *
 * @final
 * @package Crmlite_App
 * @version $Id: AnnounceController.php 56 2007-09-10 02:01:01Z etng2004 $
 * @copyright 2006-2007 ETE.CN
 * @author Yi BO  <etng2004@gmail.com>
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
require_once dirname(__FILE__) . '/AdminBaseController.php';
final class Admin_AnnounceController extends AdminBaseController
{
	public function init()
	{
		parent::init();
		$this->pushPoint('系统公告');
	}

    public function preDispatch()
	{
	    $action = $this->getRequest()->getActionName();
	    $dangeous_actions = array('add', 'remove', 'trash', 'delete','edit');
		if(in_array($action, $dangeous_actions) && $this->S_staff['ROLE'] != 'ADMIN')
		{
		    $this->feedback('只有系统管理员才能进行员工管理', '失败', $this->module_url);
		}
		parent::preDispatch();
	}

    public function indexAction()
    {
		$this->log('Annouce Index page called!');
		$this->_forward('list');
    }

    protected function getLatestAnnounce($count = 20)
    {
  		$AM = new AnnounceModel();
		$where = array(
//		'expired_at = 0 OR DATE(FROM_UNIXTIME(expired_at)) <= CURDATE()',
//		'published_at = 0 OR DATE(FROM_UNIXTIME(published_at)) >= CURDATE()',
		);
		$order = 'created_at DESC';
		return $AM->findAll($where, $order, $count);
    }

    public function addAction()
    {
    	if($this->isPost())
    	{
    		$record = $_POST['fields'];
    		$record['created_at'] = time();
    		$record['published_at'] = empty($record['published_at'])
    									?0
    									:strtotime($record['published_at']);
    		$record['expired_at'] = empty($record['expired_at'])
    									?0
    									:strtotime($record['expired_at']);
    		$AM =  new AnnounceModel();
    		if($AM->insert($record))
    		{
    			$this->removeCache();
    			$this->log($msg = "Announce #{$record['id']} created!");
    			$this->feedback($msg, '成功', $this->base_url);
    		}
    		$this->view->announce = $record;
    	}
    	$this->setTitle('发布公告');
    }

    public function editAction()
    {
    	$AM =  new AnnounceModel();
    	if($this->isPost())
    	{
    		$record = $_POST['fields'];
    		$id = $record['id'];
    		$record['published_at'] = empty($record['published_at'])
    									?0
    									:strtotime($record['published_at']);
    		$record['expired_at'] = empty($record['expired_at'])
    									?0
    									:strtotime($record['expired_at']);

    		if($AM->update($record, "id={$id}"))
    		{
    			$this->removeCache();
    			$this->log($msg = "Announce #{$record['id']} updated!");
    			$this->feedback($msg, '成功', $this->base_url);
    		}
    		$this->view->announce = $record;
    		$error = 'Update failed!';
    	}
    	/**
    	 * 若不是POST方法则为初始编辑
    	 */
    	if(empty($error))
    	{
			$id = intval($this->_getParam('id', 0));
			$this->view->announce = $AM->find($id);
    	}
    	$this->setTitle('编辑::' . $this->view->announce['title'] . '');
    }
    /**
     * 删除缓存
     *
     * 如果不指定记录ID，则默认删除所有相应的缓存
     *
     * @param int $id
     * @todo 实现删除所有的功能
     */
	protected function removeCache( $id = null )
	{
		$Cache = Zend_Registry::get('cache');
		/* @var $Cache Zend_Cache_Core */
		$Cache->remove('admin_announce_feed_data');
		$Cache->remove('announce_feed_data');
	}
	/**
	 * 分页列举公告
	 *
	 */
    public function listAction()
    {
		$AM = new AnnounceModel();
		$where = array(
//		'expired_at = 0 OR DATE(FROM_UNIXTIME(expired_at)) <= CURDATE()',
//		'published_at = 0 OR DATE(FROM_UNIXTIME(published_at)) >= CURDATE()',
		);
		$order = 'created_at DESC';
    	$pager = array(
				'curpage' 	=> $this->_getParam('page', 1),
				'perpage'	=> $this->_getParam('perpage', 10),
				'total'		=> $AM->count($where),
				'url'		=> $this->base_url . '/list',
				);
		$this->view->pager = $pager;
		$this->view->announces = $AM->paginate($where, $order, $pager['curpage'], $pager['perpage']);
		$this->setTitle('所有', $pager['total']);
    }

    public function viewAction()
    {
		$id = intval($this->_getParam('id', 1));
		$AM = new AnnounceModel();
		$this->view->announce = $announce = $AM->find($id);
		$this->setTitle($announce['title']);
    }
	/**
	 * 删除指定的记录
	 *
	 */
    public function removeAction()
    {
    	$id = intval($this->_getParam('id', 1));
		$AM = new AnnounceModel();
		if($id)
		{
			if($status = $AM->deleteById($id))
			{
				$this->removeCache();
				$this->log($msg = "Announce #{$id} deleted!");
				$this->feedback($msg, '成功', $this->base_url);
			}
		}
		echo $status;
		exit();
    }
}
